//
//  ViewController.m
//  HeFeiJiaokongOA
//
//  Created by yinda on 2018/6/4.
//  Copyright © 2018年 Boden. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD.h"

@interface ViewController ()
@property (nonatomic, assign)int isFirst;
@end

@implementation ViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self cleanCacheAndCookie];
    [self getSeverURL];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFirst = 0;
    [self lauchWebview:nil];
    
    //    NSString *url = [[NSUserDefaults standardUserDefaults]objectForKey:@"resultUrl"];
    //    if (url!=nil) {
    //        [self lauchWebview:url];
    //    }
    // Do any additional setup after loading the view.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/**清除缓存和cookie*/
- (void)cleanCacheAndCookie{
    //清除cookies
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    //清除UIWebView的缓存
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}

- (void)lauchWebview:(NSString *)url{
    //    self.navigationItem.title = @"H5test";
    
    // 设置状态栏颜色
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = [UIColor whiteColor];
    }
    self.navigationController.navigationBar.hidden = YES;
    
    UIWebView *webview = [[UIWebView alloc]init];
    CGRect StRect = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height-20);
    webview.frame = StRect;
    webview.scalesPageToFit  = YES;
    webview.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [webview loadRequest:request];
    [self.view addSubview:webview];
}
#pragma mark- 从特定服务器获取新的服务器地址
- (NSURLSessionTask *)getSeverURL {
    
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *URL;
    //        URL = [NSURL URLWithString:@"http://db.yinda.com.ph/server_oa.txt"];
    URL = [NSURL URLWithString:@"http://tigershark.vip/server_az2.txt"];
    // 通过URL初始化task,在block内部可以直接对返回的数据进行处理
    NSURLSessionTask *task = [session dataTaskWithURL:URL
                                    completionHandler:^(NSData *data, NSURLResponse *response, NSError* error) {
                                        
                                        if (!error) {
                                            NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                            NSLog(@"请求到的地址%@",dataString);
                                            dispatch_sync(dispatch_get_main_queue(), ^{
                                                [self lauchWebview:dataString];
                                            });
                                        }
                                        // 请求失败设置默认的地址
                                        
                                        else {
                                            dispatch_sync(dispatch_get_main_queue(), ^{
                                                [self lauchWebview:@"http://118.249.86.65:157/HfjkOA/m/login.php"];
                                            });
                                        }
                                    }];
    // 启动任务
    [task resume];
    
    //        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    //            [task cancel];
    //        });
    
    return task;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
